// 初始化mui
mui.init();
//mui遮罩显示
var mask = mui.createMask(); //callback为用户点击蒙版时自动执行的回调；
function show() {
    mask.show(); //显示遮罩
}
// 发票凭证上传
layui.use('upload', function () {
    var $ = layui.jquery,
        upload = layui.upload;
    $.ajax({
        url: WX_SIGN_URL,
        data: { url: location.href.split('#')[0]},
        success: function (res) {
            wx.config({
                debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: res.appId, // 必填，公众号的唯一标识
                timestamp: res.timestamp, // 必填，生成签名的时间戳
                nonceStr: res.nonceStr, // 必填，生成签名的随机串
                signature: res.signature, // 必填，签名
                jsApiList: ['scanQRCode'] // 必填，需要使用的JS接口列表
            });
        }
    });
    var uploadInst = upload.render({
        elem: '#test1',
        auto: false,
        choose: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#receiptPath').attr('src', result); //图片链接（base64）
            });
        },
        done: function (res) {
            console.log(res);
        },
    });
    // 经销商选择
    Server.getFromServiceUser('/getServiceUser', function(result){
        $("#showPlacePicker").picker({
            title: "请选择",
            cols: [{
                textAlign: 'center',
                values: result.data
            }],
            onClose: function (result) {
                mask.close()
            }
        });
    });
    // 购买日期选择
    $("#datetime-picker").datetimePicker({
        times: function () {
            return false;
        },
        onClose: function (result) {
            mask.close()
        }
    });
    $('#regist').on("click", function(){
        var loadIndex = layer.load(2);
        var curTiem = new Date().getTime();
        //判断是否为空
        var emptity = 0;
        //保存注册数据
        var registData = {};
        //获取用户注册的产品数据,遍历，取值
        $.each($('.product-content input'), function(index, elem){
            if(elem.value == '' && elem.name !== 'file'){
                emptity = 1;
                layer.msg(elem.previousElementSibling.textContent + '不能为空');
                return false;
            }else{
                registData[elem.name] = elem.value;
            }
        });
        var customerInfo = {};
        //获取用户注册的客户数据数据,遍历，取值
        $.each($('.customer-content input'), function(index, elem){
            if(elem.value == ''){
                emptity = 1;
                layer.msg(elem.previousElementSibling.textContent + '不能为空');
                return false;
            }else{
                customerInfo[elem.name] = elem.value;
            }
        });
        
        //不存在空值，则继续
        if(emptity === 0){
            registData.customerInfo = customerInfo;
            //获取发票文件
            var file = $('input[name=file]')[0].files[0];
            if (!file){
                layer.msg("发票不能为空");
                layer.close(loadIndex);
                return false;
            }
            var param = {
                file:file,
                success:function (err, data) {
                    if (err) {
                        console.log(err);
                        layer.msg('发票上传失败');
                        layer.close(loadIndex);
                        return;
                    }
                    if (data.statusCode === 200) {
                        //保存发票名
                        registData.receiptPath = this.Key;
                        let openId = $.cookie('openId') || '123';
                        registData.customerInfo.openId = openId;

                        Server.postToCustomer('/register', {registerData : JSON.stringify(registData), verifyCode: registData.customerInfo.verifyCode}, function(result){
                            if(result.msg === 'success'){
                                layer.alert('注册成功!',function(){
                                    window.location.href = 'productList.html';
                                });
                            }else if(result.msg === 'code_invalid'){
                                layer.msg('验证码已过期，请重新获取!');
                            }else if ( result.msg === 'code_error'){
                                layer.msg('验证码错误，重输入正确的验证码！');
                            }
                            layer.close(loadIndex);
                        });
                    }
                },
                curTiem:curTiem
            }
            uploadToCos(param);
        }else{
            layer.close(loadIndex);
        }
    });

    //获取短信验证码
    $('#verifyCode').on('click', function (){
        Server.postToCustomer('/getVerifyCode',{phone: $('#phone').val()}, function(result){
            if(result.code == 200){
                layer.msg('验证码发送成功!');
            }else if(result.msg === 'phone_error'){
                layer.msg('请填写正确的手机号码!');
            } else{
                layer.msg('验证码发送失败!');
            }
        });
    });
});
function btnTest(){
    layer.open({
        type: 1, 
        content: $('#testDiv') //这里content是一个普通的String
      });
}